home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / CD Audio Toolkit 1.0 / Source / CDDiscTime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  4.9 KB  |  188 lines  |  [TEXT/MPS ]

  1. /*
  2.     CDDiscTime - An XFCN to report the time on disc
  3.     ©Apple Computer, Inc. 1988
  4.     All Rights Reserved.
  5.     
  6.     88/11/08    BL°B    First Version
  7.  
  8.     To compile and link this file using Macintosh Programmer's Workshop,
  9.  
  10.     C -q2 CDDiscTime.c
  11.     link -sn Main=CDDiscTime -sn STDIO=CDDiscTime ∂
  12.          -sn INTENV=CDDiscTime -rt XFCN=42 ∂
  13.          -m CDDiscTime CDDiscTime.c.o "{CLibraries}"CRuntime.o ∂
  14.          "{CLibraries}"StdCLib.o ∂
  15.          -o HyperCommands
  16.          
  17.     This link directive puts the XCMD in the file "HyperCommands".
  18.     Substitute the name of the stack you want it in.  To move XCMDs
  19.     between stacks, use ResEdit.  They can be in an individual stack,
  20.     the Home stack, the HyperCard application, or the System File.
  21.     
  22. */
  23.  
  24. #include <cd.h>
  25.  
  26. /* prototype definitions for functions */
  27. OSErr    ThisDisc(short, long *, long *, long *);
  28. void    TimeDiff(long *, long *, long *, long, long, long, long, long, long);
  29.  
  30. /* **** WARNING:  DO NOT USE GLOBAL VARIABLES! **** */
  31.  
  32.  
  33. /************************************************************************
  34.  *
  35.  *  Function:        CDDiscTime
  36.  *
  37.  *  Purpose:        return the time of this disc.
  38.  *
  39.  *  Returns:        either 0, or an error
  40.  *                    if it's a negative number, it's an error
  41.  *
  42.  *  Side Effects:
  43.  *
  44.  *  Description:    We need one parameter:
  45.  *                    1) the ioRefNum that we got from previously calling
  46.  *                       CDOpen()
  47.  *                    call the driver with a READTOC call to find out
  48.  *                    the lead-out time.  The disc time is lead-out time
  49.  *                    minus one block.
  50.  *
  51.  ************************************************************************/
  52. pascal void
  53. CDDiscTime(paramPtr)
  54. XCmdBlockPtr    paramPtr;
  55. {
  56.     Str31    returnString;
  57.     OSErr    result;
  58.     short    ioRefNum;
  59.     Handle    refHandle;
  60.     long    discTime[3];    /* minute, second, block */
  61.     
  62.     /* Must be no parameters */
  63.     if ((paramPtr->paramCount) != 0)
  64.     {
  65.         /* Report error in parameters by returning -1 */
  66.         NumToStr(paramPtr, (long) -1, &returnString);
  67.         paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  68.         return;
  69.     }
  70.     
  71.     /* Get the global ioRefNum and convert it. */
  72.     refHandle = GetGlobal(paramPtr, GLOBALNAME);
  73.     ioRefNum = atoi(*(refHandle));
  74.     DisposHandle(refHandle);
  75.     ioRefNum &= 0xFFFF;            /* remove vRefNum; not needed. */
  76.     
  77.     result = ThisDisc(ioRefNum, &discTime[0], &discTime[1], &discTime[2]);
  78.     
  79.     TimeDiff(&discTime[0], &discTime[1], &discTime[2], 
  80.             discTime[0], discTime[1], discTime[2], 0, 0, 1);        /* disc time is lead-out time minus 1 block */
  81.     
  82.     if (result == noErr)
  83.     {
  84.         /* convert each value to a string, concatenate, & return. */
  85.         FormatString(&returnString, discTime, 3);
  86.         paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  87.     }
  88.     else
  89.     {
  90.         /* We got an error. Convert result to string & return it as error */
  91.         NumToStr(paramPtr, (long) result, &returnString);
  92.         paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  93.     }
  94. }
  95.  
  96. /************************************************************************
  97.  *
  98.  *  Function:        ThisDisc
  99.  *
  100.  *  Purpose:        return total time on this disc
  101.  *
  102.  *  Returns:        OSErr
  103.  *                    either noErr if everything was okay
  104.  *                    or some parameter error from driver call.
  105.  *
  106.  *  Side Effects:
  107.  *                    fills in totalMinute, totalSecond, totalBlock
  108.  *
  109.  *  Description:
  110.  *                    call the driver ReadTOC call to get the appropriate
  111.  *                    information.
  112.  *
  113.  ************************************************************************/
  114. OSErr
  115. ThisDisc(refNum, minute, second, block)
  116. short    refNum;
  117. long    *minute;
  118. long    *second;
  119. long    *block;
  120. {
  121.     CDParam    myPB;
  122.     OSErr    result;
  123.     
  124.     myPB.ioCompletion = 0;
  125.     myPB.ioNamePtr = (char *) 0;
  126.     myPB.ioVRefNum = 1;
  127.     myPB.ioCRefNum = refNum;
  128.     myPB.csCode = READTOC;
  129.     myPB.csParam[0] = 0;
  130.     myPB.csParam[1] = 2;     /* request lead-out time */
  131.     
  132.     result = PBControl(&myPB, false);
  133.     
  134.     if (result == noErr)
  135.     {
  136.         *minute = (long) BCD2DECIMAL(myPB.csParam[0]);
  137.         *second = (long) BCD2DECIMAL(myPB.csParam[1]);
  138.         *block = (long) BCD2DECIMAL(myPB.csParam[2]);
  139.     }
  140.     return result;
  141. }
  142.  
  143. /************************************************************************
  144.  *
  145.  *  Function:        TimeDiff
  146.  *
  147.  *  Purpose:        calculate difference between two times
  148.  *
  149.  *  Returns:        nothing
  150.  *
  151.  *  Side Effects:    fills rMinute, rSecond, rBlock
  152.  *
  153.  *  Description:    convert the absolute times designated by
  154.  *                    {m1, s1, f1} and {m2, s2, f2} to absolute
  155.  *                    blocks.  Subtract the second time from the
  156.  *                    first.  Convert the result back to {m, s, f}
  157.  *                    format.
  158.  *
  159.  ************************************************************************/
  160. void
  161. TimeDiff(rMinute, rSecond, rBlock, m1, s1, f1, m2, s2, f2)
  162. long    *rMinute;
  163. long    *rSecond;
  164. long    *rBlock;
  165. long    m1, s1, f1, m2, s2, f2;
  166. {
  167.     long    time1, time2;
  168.     long    minute, second, block;
  169.     
  170.     time1 = f1 + (s1 * BLOCKSEC) + (m1 * BLOCKMIN);
  171.     time2 = f2 + (s2 * BLOCKSEC) + (m2 * BLOCKMIN);
  172.     
  173.     time1 = time1 - time2;
  174.     
  175.     minute = time1 / BLOCKMIN;
  176.     time1 = time1 - (minute * BLOCKMIN);
  177.     
  178.     second = time1 / BLOCKSEC;
  179.     block = time1 - (second * BLOCKSEC);
  180.     
  181.     *rMinute = minute;
  182.     *rSecond = second;
  183.     *rBlock = block;
  184. }
  185.  
  186. /* C routines for HyperCard callbacks */
  187. #include <XCmdGlue.inc.c>
  188.